package com.smart.admin.controller;

import com.smart.admin.model.ConfigHistoryVO;
import com.smart.admin.model.ConfigRequest;
import com.smart.admin.service.ConfigService;
import com.smart.admin.test.TestComponent;
import com.smart.admin.utils.Result;
import com.smart.config.utils.GsonTool;
import com.smart.config.constant.RedisConstant;
import com.smart.config.model.ConfigHistory;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author xiaoqiang
 * @Version ConfigController.java, v 0.1 2025年03月18日 14:46 xiaoqiang
 * @Description: TODO
 */
@RestController
@RequestMapping("/config")
public class ConfigController {


    private ConfigService configService;


    public ConfigController(ConfigService configService) {
        this.configService = configService;
    }


    /**
     * 获取配置
     *
     * @param appName
     * @param group
     * @param dataId
     * @return
     */
    @GetMapping("/getConfig")
    public Result<Object> getConfig(@RequestParam("appName") String appName,
                                    @RequestParam("group") String group,
                                    @RequestParam("dataId") String dataId) {
        Object value = configService.getConfig(appName, group, dataId);
        if (value == null) {
            return Result.fail("100", "配置不存在");

        }
        return Result.success(value);
    }


    /**
     * 保存配置
     *
     * @param configRequest
     * @return 返回订阅机器的数量
     */
    @PostMapping("/save")
    public Result<Long> save(@RequestBody ConfigRequest configRequest) {
        if (StringUtils.isBlank(configRequest.getAppName())) {
            return Result.fail("100", "应用名称不能为空");
        }
        if (StringUtils.isBlank(configRequest.getGroup())) {
            return Result.fail("100", "组名不能为空");
        }
        if (StringUtils.isBlank(configRequest.getDataId())) {
            return Result.fail("100", "数据ID不能为空");
        }
        if (configRequest.getContent() == null) {
            return Result.fail("100", "配置内容不能为空");
        }
        long result = configService.saveConfig1(configRequest);
        return Result.success(result);
    }


    @GetMapping("/getAllHistoryConfig")
    public Result<List<ConfigHistoryVO>> getAllHistoryConfig(
            @RequestParam("appName") String appName,
            @RequestParam("group") String group,
            @RequestParam("dataId") String dataId) {
        if (appName == null) {
            throw new RuntimeException("appName is null");
        }
        if (group == null) {
            throw new RuntimeException("group is null");
        }
        if (dataId == null) {
            throw new RuntimeException("dataId is null");
        }

        List<ConfigHistoryVO> configHistoryList = configService.getConfigVersions(appName, group, dataId);

        return Result.success(configHistoryList);
    }


    @PostMapping("/delete")
    public Result<Integer> delete(@RequestBody ConfigRequest configRequest) {
        if (StringUtils.isBlank(configRequest.getAppName())) {
            return Result.fail("100", "应用名称不能为空");
        }
        if (StringUtils.isBlank(configRequest.getGroup())) {
            return Result.fail("100", "组名不能为空");
        }
        if (StringUtils.isBlank(configRequest.getDataId())) {
            return Result.fail("100", "数据ID不能为空");
        }
        Integer result = configService.deleteConfig(configRequest.getAppName(), configRequest.getGroup(), configRequest.getDataId());
        return Result.success(result);
    }
}
